package cn.com.open.opensaas.answerservice.service.impl;

import cn.com.open.opensaas.answerservice.common.ReturnModel;
import cn.com.open.opensaas.answerservice.enums.ResultType;
import cn.com.open.opensaas.answerservice.mapper.CourseItemBankMapper;
import cn.com.open.opensaas.answerservice.model.CourseItemBank;
import cn.com.open.opensaas.answerservice.model.pager.Item;
import cn.com.open.opensaas.answerservice.model.pager.PagerContentJsonModel;
import cn.com.open.opensaas.answerservice.model.pager.TestPaperContent;
import cn.com.open.opensaas.answerservice.model.question.AnswerQuestionLog;
import cn.com.open.opensaas.answerservice.model.question.QuestionContent;
import cn.com.open.opensaas.answerservice.model.question.QuestionContentJsonModel;
import cn.com.open.opensaas.answerservice.service.AnswerQuestionService;
import cn.com.open.opensaas.answerservice.service.QuestionContentService;
import cn.com.open.opensaas.answerservice.service.QuestionJudgeService;
import cn.com.open.opensaas.answerservice.util.SnowflakeIdWorker;
import cn.com.open.opensaas.answerservice.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.expression.spel.ast.NullLiteral;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AnswerQuestionServiceImpl implements AnswerQuestionService {

    @Autowired
    private CourseItemBankMapper courseItemBankMapper;

    @Autowired
    private QuestionContentService questionContentService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private QuestionJudgeService questionJudgeService;

    /**
     * 安全认证
     * @param itemBankId  题库ID
     * @param appkey 应用ID
     * @return
     */
    @Override
    public CourseItemBank findCourseItemBankOne(String itemBankId, String appkey) {
        return courseItemBankMapper.findCourseItemBankOne(itemBankId,appkey);
    }

    /**
     * 试卷--保存作答记录
     * @param answerQuestionLog
     * @param testPaperContent
     * @return
     */
    @Override
    public ReturnModel save(AnswerQuestionLog answerQuestionLog, TestPaperContent testPaperContent) {
        List<AnswerQuestionLog> mongoList = new ArrayList<>();
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(0,0);

        List<QuestionContentJsonModel> answerList = answerQuestionLog.getItems();
        long uniqueId = snowflakeIdWorker.nextId();
        //去除转义字符
        String testPaperContentJson = StringEscapeUtils.unescapeJava(testPaperContent.getContentJson());
        //反序列化成map
        PagerContentJsonModel pagerContentJsonModel =  JSON.parseObject(testPaperContentJson,new TypeReference<PagerContentJsonModel>() {});
        List<Item> items = pagerContentJsonModel.getItems();
        //查询作答题的详细信息
        List<QuestionContent> dbQuestionList = questionContentService.batchFindDbQuestion(answerList);
        for (Item item : items) {
            for (QuestionContent dbQuestion : dbQuestionList) {
                if ((dbQuestion.getQuestionID()).equalsIgnoreCase(item.getI1())) {
                    AnswerQuestionLog mongoAnswerQuestionLog = (AnswerQuestionLog) answerQuestionLog.clone();

                    //去除转义字符
                    String questionContentJson = StringEscapeUtils.unescapeJava(dbQuestion.getContentJson());
                    //反序列化成map
                    QuestionContentJsonModel questionContentJsonModel = JSON.parseObject(questionContentJson, new TypeReference<QuestionContentJsonModel>() {});

                    mongoAnswerQuestionLog.setUniqueId(uniqueId);
                    List<QuestionContentJsonModel> mongoItems = new ArrayList<>();
                    QuestionContentJsonModel mongoQuestionModel = loopQuestion(questionContentJsonModel, answerList);
                    mongoQuestionModel.setI9(item.getI9());
                    mongoItems.add(mongoQuestionModel);
                    mongoAnswerQuestionLog.setItems(mongoItems);
                    mongoList.add(mongoAnswerQuestionLog);
                }
            }
        }
        System.out.println(JSON.toJSONString(mongoList));
        mongoTemplate.insert(mongoList,"answerquestionlog");

        return questionJudgeService.questionJudge(uniqueId);
    }

    /**
     * 试题--保存作答记录
     * @param answerQuestionLog
     * @return
     */
    @Override
    public ReturnModel save(AnswerQuestionLog answerQuestionLog) {
        List<AnswerQuestionLog> mongoList = new ArrayList<>();
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(0,0);

        List<QuestionContentJsonModel> answerList = answerQuestionLog.getItems();
        long uniqueId = snowflakeIdWorker.nextId();
        //查询作答题的详细信息
        List<QuestionContent> dbQuestionList = questionContentService.batchFindDbQuestion(answerList);

        for (QuestionContent dbQuestion : dbQuestionList) {
            AnswerQuestionLog mongoAnswerQuestionLog = (AnswerQuestionLog) answerQuestionLog.clone();
            //去除转义字符
            String questionContentJson = StringEscapeUtils.unescapeJava(dbQuestion.getContentJson());
            //反序列化成map
            QuestionContentJsonModel questionContentJsonModel = JSON.parseObject(questionContentJson, new TypeReference<QuestionContentJsonModel>() {});

            mongoAnswerQuestionLog.setUniqueId(uniqueId);
            List<QuestionContentJsonModel> mongoItems = new ArrayList<>();
            mongoItems.add(loopQuestion(questionContentJsonModel, answerList));
            mongoAnswerQuestionLog.setItems(mongoItems);
            mongoList.add(mongoAnswerQuestionLog);
        }

        mongoTemplate.insert(mongoList,"answerquestionlog");
        return questionJudgeService.questionJudge(uniqueId);
    }

    /**
     * 循环Question对item添加I15或Sub属性
     *
     * @param item
     */
    private QuestionContentJsonModel loopQuestion(QuestionContentJsonModel item, List<QuestionContentJsonModel> answerList) {

        //循环questionList获取I1为itemId的question,将I15添加到item中
        for (QuestionContentJsonModel question : answerList) {
            if (item.getI1().equalsIgnoreCase(question.getI1())) {//I1一致
                if(question.getI9() != null){
                    double i9 = question.getI9();
                    item.setI9(i9);
                }
                List<String> i15 = question.getI15();
                List<QuestionContentJsonModel> sub = question.getSub();
                if(null == sub || sub.isEmpty()){
                    //学生传递的数据有子集(有I15)
                    item.setI15(i15);
                }else {
                    //学生传递数据有子试题
                    List<QuestionContentJsonModel> subQuestions = question.getSub();
                    List<QuestionContentJsonModel> subItems = item.getSub();
                    List<QuestionContentJsonModel> mongoSubs = new ArrayList<>();
                    for (QuestionContentJsonModel subItem : subItems){
                        mongoSubs.add(loopQuestion(subItem, subQuestions));
                    }
                    item.setSub(mongoSubs);
                }
                break;
            }
        }
        return item;
    }


    public static void main(String[] args) {
        String a = "1";
        for (int i=0;i<10;i++){
            String b =a;
            b="2";
            System.out.println(b+"--------------"+a);

        }



    }
}
